<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="邮件名称" prop="project">
        <el-input v-model="queryParams.project" placeholder="请输入邮件名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="发件人" prop="createBy">
        <el-input v-model="queryParams.createBy" placeholder="请输入发件人" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" filterable>
          <el-option v-for="dict in typeOptions" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" filterable>
          <el-option v-for="dict in statusOptions" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDelete">删除</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="inboxList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" width="50"></el-table-column>
      <!-- <el-table-column label="编号" align="center" prop="id" /> -->
      <el-table-column label="邮件名称" align="center" prop="project" />
      <el-table-column label="发件人" align="center" prop="createBy" />
      <el-table-column label="部门名称" align="center" prop="deptName" />

      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <span v-if="scope.row.type == 0">普通文件</span>
          <span v-else>机密文件</span>
        </template>
      </el-table-column>

      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 0">未读</span>
          <span v-else>已读</span>
        </template>
      </el-table-column>
      <el-table-column label="发件时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-document" @click="changeEmailStatus(scope.row)">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getInboxList" />

    <!-- 添加或修改邮箱对话框 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="500px" append-to-body>
      <el-form :model="formData" :rules="rules" size="medium" label-width="80px">

        <el-form-item label="发件人" prop="createBy">
          <el-input v-model="formData.createBy" placeholder="请输入发件人" clearable :style="{ width: '100%' }" disabled>
          </el-input>
        </el-form-item>

        <el-form-item label="主题" prop="project">
          <el-input v-model="formData.project" placeholder="请输入主题" clearable :style="{ width: '100%' }" disabled>
          </el-input>
        </el-form-item>

        <el-form-item label="类型" prop="type">
          <el-select v-model="formData.type" placeholder="请选择类型" clearable filterable :style="{ width: '100%' }"
            disabled>
            <el-option v-for="dict in typeOptions" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>

        <el-form-item label="附件" prop="address">
          <file-upload v-model="formData.address" disabled />
        </el-form-item>

        <el-form-item label="正文" prop="content">
          <el-input v-model="formData.content" type="textarea" placeholder="请输入" :maxlength="255"
            :autosize="{ minRows: 8, maxRows: 8 }" :style="{ width: '100%' }" disabled></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getInbox, selectInboxList, changeStatus, selectByCondition, updateSymbol } from "@/api/email/inbox";

export default {
  name: "Inbox",
  dicts: ['email_table_status', 'email_table_type'],
  data() {
    return {
      // 展示当前用户收件箱的信息

      // 表单当中的 formData
      formData: {
        createBy: '',
        project: undefined,
        type: '',
        content: undefined,
        deptId: '',
        address: null,
      },
      // 嵌套表单属性
      dialogTableVisible: false,
      dialogFormVisible: false,
      // form: {
      //   name: '',
      //   region: '',
      //   date1: '',
      //   date2: '',
      //   delivery: false,
      //   type: [],
      //   resource: '',
      //   desc: ''
      // },
      formLabelWidth: '120px',
      // 状态选择
      statusOptions: [
        { value: 0, label: '未读' },
        { value: 1, label: '已读' },
      ],
      // 类型选择
      typeOptions: [
        { value: 0, label: '普通文件' },
        { value: 1, label: '机密文件' },
      ],


      value: '',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 邮箱表格数据
      // 表格数据
      inboxList: [{
        // id: '',
        project: '',
        createBy: '',
        deptName: '',
        type: '',
        status: '',
        createTime: ''
      }],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        project: null,
        createBy: null,
        type: '',
        status: '',
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },

  created() {
    this.getInboxList();
  },
  methods: {
    // 点击查看按钮后未读状态变为已读，并且回显邮件内容
    changeEmailStatus(row) {
      const id = row.id;
      // getInbox(id).then((response) => {
      //   this.formData = response.data;
      //   this.title = "获取回显邮件内容";
      // });
      this.formData = row;
      this.title = "获取回显邮件内容";
      this.dialogFormVisible = true;
      changeStatus(id).then((response) => {
        // 假设后端只返回成功或失败的状态，不返回整个表单数据  
        if (response.data.code === 200) { // 假设200是成功的状态码  
          this.form = response.data;
          this.title = "修改邮件状态";
          console.log('邮件状态已更新为已读');
        } else {
          console.error('更新邮件状态失败:', response.data.message);
        }
      })
    },

    /** 查询当前用户收件箱列表 */
    getInboxList() {
      this.loading = true;
      selectInboxList(this.queryParams).then(response => {
        this.inboxList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 根据条件查询查收件箱内容 */
    getConditionList() {
      this.loading = true;
      selectByCondition(this.queryParams).then(response => {
        this.inboxList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 重置邮箱表
    getRest() {
      this.loading = true;
      selectInboxList(this.queryParams).then(response => {
        this.inboxList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 关闭按钮
    cancel() {
      // this.open = false;
      this.dialogFormVisible = false;
      this.resetForm("queryForm");
      this.handleRestQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getConditionList();
    },

    handleRestQuery() {
      this.queryParams.pageNum = 1;
      this.getRest();
    },

    resetQuery() {
      this.resetForm("queryForm");
      this.handleRestQuery();
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      this.handleRestQuery();
      // 确保使用 row.id，假设这是你要删除的单个数据项的 ID  
      const id = row.id || this.ids;
      // 使用箭头函数来保持 this 的上下文  
      this.$modal.confirm('是否确认删除邮箱发件箱编号为"' + id + '"的数据项？').then(() => {
        // 调用 updateSymbol 函数，假设它负责删除操作  
        return updateSymbol(id);
      }).then(() => {
        this.getOutBoxList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },


    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
  }
};
</script>
